package bpui.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.graphics.Image;
import org.wat.wcy.isi.mmazur.bp.io.IdDesc;
import org.wat.wcy.isi.mmazur.bp.io.KeyValuePair;
import org.wat.wcy.isi.mmazur.bp.io.Node;
import org.wat.wcy.isi.mmazur.bp.io.NodeType;

public class ModelUtils {
	
	public final static String ID_NULL = "00000000-0000-0000-0000-000000000000";
	
	public static String getNodeLabel(final Node node) {
		StringBuilder strb = new StringBuilder();
		org.wat.wcy.isi.mmazur.bp.io.model.enums.NodeType nt = 
			org.wat.wcy.isi.mmazur.bp.io.model.enums.NodeType.valueOf(node.getType().name());
		strb.append(nt.getName());
		if (StringUtils.isNotEmpty(node.getName())) {
			strb.append("('");
			strb.append(node.getName());
			strb.append("')");
		}
		return strb.toString();
	}
	
	public static Image getImage(NodeType type) {
		return RCPUtil.getImage("bp-ui", "/icons/bpmn/"
				+ type.toString().toLowerCase() + ".png");
	}

	public static boolean isNull(IdDesc laneId) {
		return StringUtils.equalsIgnoreCase(laneId.getData(), ID_NULL);
	}

	public static Map<String, String> convertToHashMap(
			List<KeyValuePair> list) {
		if (list == null) {
			return null;
		}
		Map<String, String> result = new HashMap<String, String> ();
		for (KeyValuePair kpv : list) {
			result.put(kpv.getKey(), kpv.getValue());
		}
		return result;
	}

	public static String toString(IdDesc id) {
		return id != null ? id.getData() : ID_NULL;
	}
}
